package com.linuxgt.baidu.zhidao;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by LIUTENG on 2015-05-13.
 */
public class LikeDos {

    /** Creates a new instance ofN11Code */

    /**
     * @param args the commandline arguments
     */
    public static void main(String[] args) {

        String str = "/";

        File curr = new File(str);

        try {
            while (str != null) {
                System.out.print(curr.getPath());
                System.out.print(">");
                BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
                str = in.readLine();
                if (str.startsWith("exit")) {
                    return;
                } else if (str.startsWith("help")) {
                    System.out.println("列出目录中的所有文件和目录          dir");
                    System.out.println("删除文件                            del  欲删除文件名");
                    System.out.println("修改文件名                          rename  源文件名  新文件名");
                    System.out.println("切换到上一层目录                    cd..");
                    System.out.println("切换到当前目录的子目录              cd 子目录名");
                    System.out.println("切换到其它目录                      cd 全目录名");
                } else if ("dir".equals(str)) {//列出目录
                    File file = new File(System.getProperty("user.dir"));
                    File files[] = file.listFiles();
                    for (int i = 0; i < files.length; i++) {
                        System.out.println(files[i].getName());
                    }
                } else if (str.startsWith("cd")) {
                    String[] params = str.split(" ");
                    File Dir = new File(params[1]);
                    File file = new File(System.getProperty("user.dir"));
                    if (Dir.isDirectory() && "..".equals(Dir))//转到父目录
                        System.setProperty("user.dir", String.valueOf(file.getParentFile()));
                    else if (Dir.isDirectory() && Dir.getName().indexOf("\\") != -1) {//转子目录
                        System.setProperty("user.dir", String.valueOf(Dir));
                    } else if (Dir.isDirectory()) {//转任意目录
                        System.setProperty("user.dir", String.valueOf(Dir.getAbsolutePath()));
                    }
                } else if (str.startsWith("del")) {//删除文件
                    String[] params = str.split(" ");
                    File delFile = new File(params[1]);
                    if (!delFile.isDirectory())//非目录删除
                        System.out.println(delFile.delete());
                } else if (str.startsWith("rename")) {
                    String[] params = str.split(" ");
                    File oldName = new File(params[1]);
                    File newName = new File(params[2]);
                    if (!oldName.isDirectory())//非目录重命名
                        System.out.println(oldName.renameTo(newName));
                } else
                    System.out.println("Wrong Order!");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
